﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Model
{
    [Serializable()]
    public class ThuChiBO : Csla.BusinessBase<ThuChiBO>
    {
        private Hashtable hstOldValue = new Hashtable();
        public Hashtable HstOldValue
        {
            get { return this.hstOldValue; }
            set { this.hstOldValue = value; }
        }

        #region Business Properties

        private int? _ThuChiID = null;
        public int? ThuChiID
        {
            get { return this._ThuChiID; }
            set { this._ThuChiID = value; PropertyHasChanged("ThuChiID"); }
        }

        private int? _LoaiPhieu = Session.ThuChi_LoaiPhieu;
        public int? LoaiPhieu
        {
            get { return this._LoaiPhieu; }
            set { this._LoaiPhieu = value; PropertyHasChanged("LoaiPhieu"); }
        }

        private string _BoPhan = null;
        public string BoPhan
        {
            get { return this._BoPhan; }
            set { this._BoPhan = value; PropertyHasChanged("BoPhan"); }
        }

        private DateTime? _NgayLapPhieu = Session.ThuChi_DotKhaiBao;
        public DateTime? NgayLapPhieu
        {
            get { return this._NgayLapPhieu; }
            set { this._NgayLapPhieu = value; PropertyHasChanged("NgayLapPhieu"); }
        }

        private DateTime? _NgayKyNhan = Session.ThuChi_DotKhaiBao;
        public DateTime? NgayKyNhan
        {
            get { return this._NgayKyNhan; }
            set { this._NgayKyNhan = value; PropertyHasChanged("NgayKyNhan"); }
        }

        private string _QuyenSo = null;
        public string QuyenSo
        {
            get { return this._QuyenSo; }
            set { this._QuyenSo = value; PropertyHasChanged("QuyenSo"); }
        }

        private int? _HoaDonBanID = null;
        public int? HoaDonBanID
        {
            get { return this._HoaDonBanID; }
            set { this._HoaDonBanID = value; PropertyHasChanged("HoaDonBanID"); }
        }

        private string _So = null;
        public string So
        {
            get { return this._So; }
            set { this._So = value; PropertyHasChanged("So"); }
        }

        private string _No = null;
        public string No
        {
            get { return this._No; }
            set { this._No = value; PropertyHasChanged("No"); }
        }

        private string _Co = null;
        public string Co
        {
            get { return this._Co; }
            set { this._Co = value; PropertyHasChanged("Co"); }
        }

        private int? _KhachHangID = null;
        public int? KhachHangID
        {
            get { return this._KhachHangID; }
            set { this._KhachHangID = value; PropertyHasChanged("KhachHangID"); }
        }

        private bool? _LaKhachHang = true;
        public bool? LaKhachHang
        {
            get { return this._LaKhachHang; }
            set { this._LaKhachHang = value; PropertyHasChanged("LaKhachHang"); }
        }

        private string _HoTen = null;
        public string HoTen
        {
            get { return this._HoTen; }
            set { this._HoTen = value; PropertyHasChanged("HoTen"); }
        }

        private string _DiaChi = null;
        public string DiaChi
        {
            get { return this._DiaChi; }
            set { this._DiaChi = value; PropertyHasChanged("DiaChi"); }
        }

        private string _LyDo = null;
        public string LyDo
        {
            get { return this._LyDo; }
            set { this._LyDo = value; PropertyHasChanged("LyDo"); }
        }

        private double? _SoTien = null;
        public double? SoTien
        {
            get { return this._SoTien; }
            set
            {
                this._SoTien = value;
                if (this._SoTien == null)
                    this._DaNhanDuSoTien = "";
                else
                    this._DaNhanDuSoTien = StaticUtil.ConvertMoney2Str(this._SoTien);
                PropertyHasChanged("SoTien");
            }
        }

        private string _ChungTuGocKemTheo = null;
        public string ChungTuGocKemTheo
        {
            get { return this._ChungTuGocKemTheo; }
            set { this._ChungTuGocKemTheo = value; PropertyHasChanged("ChungTuGocKemTheo"); }
        }

        private int? _GiamDocID = Session.ThuChi_GiamDocID;
        public int? GiamDocID
        {
            get { return this._GiamDocID; }
            set { this._GiamDocID = value; PropertyHasChanged("GiamDocID"); }
        }

        private int? _KeToanTruongID = Session.ThuChi_KeToanID;
        public int? KeToanTruongID
        {
            get { return this._KeToanTruongID; }
            set { this._KeToanTruongID = value; PropertyHasChanged("KeToanTruongID"); }
        }

        private string _NguoiNopTien = null;
        public string NguoiNopTien
        {
            get { return this._NguoiNopTien; }
            set { this._NguoiNopTien = value; PropertyHasChanged("NguoiNopTien"); }
        }

        private int? _NguoiLapPhieuID = Session.LoginID;
        public int? NguoiLapPhieuID
        {
            get { return this._NguoiLapPhieuID; }
            set { this._NguoiLapPhieuID = value; PropertyHasChanged("NguoiLapPhieuID"); }
        }

        private int? _ThuQuyID = Session.ThuChi_ThuQuyID;
        public int? ThuQuyID
        {
            get { return this._ThuQuyID; }
            set { this._ThuQuyID = value; PropertyHasChanged("ThuQuyID"); }
        }

        private string _DaNhanDuSoTien = null;
        public string DaNhanDuSoTien
        {
            get { return this._DaNhanDuSoTien; }
            set { this._DaNhanDuSoTien = value; PropertyHasChanged("DaNhanDuSoTien"); }
        }

        private double? _TyGiaNgoaiTe = null;
        public double? TyGiaNgoaiTe
        {
            get { return this._TyGiaNgoaiTe; }
            set { this._TyGiaNgoaiTe = value; PropertyHasChanged("TyGiaNgoaiTe"); }
        }

        private double? _SoTienQuyDoi = null;
        public double? SoTienQuyDoi
        {
            get { return this._SoTienQuyDoi; }
            set { this._SoTienQuyDoi = value; PropertyHasChanged("SoTienQuyDoi"); }
        }

        private bool? _DaKhoa = false;
        public bool? DaKhoa
        {
            get { return this._DaKhoa; }
            set { this._DaKhoa = value; PropertyHasChanged("DaKhoa"); }
        }

        private int? _USRC = Session.LoginID;
        public int? USRC
        {
            get { return this._USRC; }
            set { this._USRC = value; PropertyHasChanged("USRC"); }
        }

        private DateTime? _DATC = DateTime.MaxValue;
        public DateTime? DATC
        {
            get { return this._DATC; }
            set { this._DATC = value; PropertyHasChanged("DATC"); }
        }

        private int? _USRM = null;
        public int? USRM
        {
            get { return this._USRM; }
            set { this._USRM = value; PropertyHasChanged("USRM"); }
        }

        private DateTime? _DATM = null;
        public DateTime? DATM
        {
            get { return this._DATM; }
            set { this._DATM = value; PropertyHasChanged("DATM"); }
        }

        private bool? _DELE = false;
        public bool? DELE
        {
            get { return this._DELE; }
            set { this._DELE = value; PropertyHasChanged("DELE"); }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; }
        }

        private int? _Thang = Session.ThuChi_DotKhaiBao.Month;
        public int? Thang
        {
            get { return this._Thang; }
            set { this._Thang = value; }
        }

        private int? _Nam = Session.ThuChi_DotKhaiBao.Year;
        public int? Nam
        {
            get { return this._Nam; }
            set { this._Nam = value; }
        }

        protected override object GetIdValue()
        {
            return _ThuChiID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newThuChiID = -1;
        private ThuChiBO()
        {
            _ThuChiID = newThuChiID--;
        }

        public ThuChiBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public ThuChiBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static ThuChiBO NewRootThuChiBO()
        {
            return DataPortal.Create<ThuChiBO>(new RootCriteria());
        }

        internal static ThuChiBO NewChildThuChiBO()
        {
            return DataPortal.Create<ThuChiBO>(new ChildCriteria());
        }

        public static ThuChiBO GetRootThuChiBO(int _ThuChiID)
        {
            return DataPortal.Fetch<ThuChiBO>(new RootCriteria(_ThuChiID));
        }

        public static ThuChiBO GetRootThuChiBOByWhere(string where)
        {
            return DataPortal.Fetch<ThuChiBO>(where);
        }

        internal static ThuChiBO GetChildThuChiBO(SqlDataReader dataReader)
        {
            return new ThuChiBO(dataReader);
        }

        public static void DeleteThuChiBO(int _ThuChiID)
        {
            DataPortal.Delete(new RootCriteria(_ThuChiID));
        }

        public static void DeleteThuChiBO(int _ThuChiID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_ThuChiID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int ThuChiID;
            public bool directDelete = false;
            public RootCriteria(int _ThuChiID)
            {
                this.ThuChiID = _ThuChiID;
                this.directDelete = false;
            }

            public RootCriteria(int _ThuChiID, bool directDelete)
            {
                this.ThuChiID = _ThuChiID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int ThuChiID;
            public bool directDelete = false;
            public ChildCriteria(int _ThuChiID)
            {
                this.ThuChiID = _ThuChiID;
                this.directDelete = false;
            }

            public ChildCriteria(int _ThuChiID, bool directDelete)
            {
                this.ThuChiID = _ThuChiID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("ThuChiBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[ThuChiID] = " + criteria.ThuChiID);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void This_Fetch(string where)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("ThuChiBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void Fetch(SqlDataReader dataReader)
        {
            if (!(dataReader["ThuChiID"] is DBNull))
                this._ThuChiID = (int)dataReader["ThuChiID"];
            if (!(dataReader["LoaiPhieu"] is DBNull))
                this._LoaiPhieu = (int)dataReader["LoaiPhieu"];
            if (!(dataReader["BoPhan"] is DBNull))
                this._BoPhan = (string)dataReader["BoPhan"];
            if (!(dataReader["NgayLapPhieu"] is DBNull))
                this._NgayLapPhieu = (DateTime)dataReader["NgayLapPhieu"];

            if (this._NgayLapPhieu != null)
            {
                this.Thang = this._NgayLapPhieu.Value.Month;
                this.Nam = this._NgayLapPhieu.Value.Year;
            }

            if (!(dataReader["NgayKyNhan"] is DBNull))
                this._NgayKyNhan = (DateTime)dataReader["NgayKyNhan"];
            if (!(dataReader["QuyenSo"] is DBNull))
                this._QuyenSo = (string)dataReader["QuyenSo"];
            if (!(dataReader["HoaDonBanID"] is DBNull))
                this._HoaDonBanID = (int)dataReader["HoaDonBanID"];
            if (!(dataReader["So"] is DBNull))
                this._So = (string)dataReader["So"];
            if (!(dataReader["No"] is DBNull))
                this._No = (string)dataReader["No"];
            if (!(dataReader["Co"] is DBNull))
                this._Co = (string)dataReader["Co"];
            if (!(dataReader["KhachHangID"] is DBNull))
                this._KhachHangID = (int)dataReader["KhachHangID"];
            if (!(dataReader["LaKhachHang"] is DBNull))
                this._LaKhachHang = (bool)dataReader["LaKhachHang"];
            if (!(dataReader["HoTen"] is DBNull))
                this._HoTen = (string)dataReader["HoTen"];
            if (!(dataReader["DiaChi"] is DBNull))
                this._DiaChi = (string)dataReader["DiaChi"];
            if (!(dataReader["LyDo"] is DBNull))
                this._LyDo = (string)dataReader["LyDo"];
            if (!(dataReader["SoTien"] is DBNull))
                this._SoTien = (double)dataReader["SoTien"];
            if (!(dataReader["ChungTuGocKemTheo"] is DBNull))
                this._ChungTuGocKemTheo = (string)dataReader["ChungTuGocKemTheo"];
            if (!(dataReader["GiamDocID"] is DBNull))
                this._GiamDocID = (int)dataReader["GiamDocID"];
            if (!(dataReader["KeToanTruongID"] is DBNull))
                this._KeToanTruongID = (int)dataReader["KeToanTruongID"];
            if (!(dataReader["NguoiNopTien"] is DBNull))
                this._NguoiNopTien = (string)dataReader["NguoiNopTien"];
            if (!(dataReader["NguoiLapPhieuID"] is DBNull))
                this._NguoiLapPhieuID = (int)dataReader["NguoiLapPhieuID"];
            if (!(dataReader["ThuQuyID"] is DBNull))
                this._ThuQuyID = (int)dataReader["ThuQuyID"];
            if (!(dataReader["DaNhanDuSoTien"] is DBNull))
                this._DaNhanDuSoTien = (string)dataReader["DaNhanDuSoTien"];
            if (!(dataReader["TyGiaNgoaiTe"] is DBNull))
                this._TyGiaNgoaiTe = (double)dataReader["TyGiaNgoaiTe"];
            if (!(dataReader["SoTienQuyDoi"] is DBNull))
                this._SoTienQuyDoi = (double)dataReader["SoTienQuyDoi"];
            if (!(dataReader["DaKhoa"] is DBNull))
                this._DaKhoa = (bool)dataReader["DaKhoa"];
            if (!(dataReader["USRC"] is DBNull))
                this._USRC = (int)dataReader["USRC"];
            if (!(dataReader["DATC"] is DBNull))
                this._DATC = (DateTime)dataReader["DATC"];
            if (!(dataReader["USRM"] is DBNull))
                this._USRM = (int)dataReader["USRM"];
            if (!(dataReader["DATM"] is DBNull))
                this._DATM = (DateTime)dataReader["DATM"];
            if (!(dataReader["DELE"] is DBNull))
                this._DELE = (bool)dataReader["DELE"];

            GetOldData();
        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
            this._USRC = Session.LoginID;
            this._DATC = DateTime.MaxValue;
            this._USRM = null;
            this._DATM = null;
            this._DELE = false;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("ThuChiBO_InsertBO");
            db.AddInParameter(cmd, "@LoaiPhieu", SqlDbType.Int, this._LoaiPhieu);
            db.AddInParameter(cmd, "@BoPhan", SqlDbType.NVarChar, this._BoPhan);
            db.AddInParameter(cmd, "@NgayLapPhieu", SqlDbType.DateTime, this._NgayLapPhieu);
            db.AddInParameter(cmd, "@NgayKyNhan", SqlDbType.DateTime, this._NgayKyNhan);
            db.AddInParameter(cmd, "@QuyenSo", SqlDbType.NVarChar, this._QuyenSo);
            db.AddInParameter(cmd, "@HoaDonBanID", SqlDbType.Int, this._HoaDonBanID);
            db.AddInParameter(cmd, "@So", SqlDbType.NVarChar, this._So);
            db.AddInParameter(cmd, "@No", SqlDbType.NVarChar, this._No);
            db.AddInParameter(cmd, "@Co", SqlDbType.NVarChar, this._Co);
            db.AddInParameter(cmd, "@KhachHangID", SqlDbType.Int, this._KhachHangID);
            db.AddInParameter(cmd, "@LaKhachHang", SqlDbType.Bit, this._LaKhachHang);
            db.AddInParameter(cmd, "@HoTen", SqlDbType.NVarChar, this._HoTen);
            db.AddInParameter(cmd, "@DiaChi", SqlDbType.NVarChar, this._DiaChi);
            db.AddInParameter(cmd, "@LyDo", SqlDbType.NVarChar, this._LyDo);
            db.AddInParameter(cmd, "@SoTien", SqlDbType.Float, this._SoTien);
            db.AddInParameter(cmd, "@ChungTuGocKemTheo", SqlDbType.NVarChar, this._ChungTuGocKemTheo);
            db.AddInParameter(cmd, "@GiamDocID", SqlDbType.Int, this._GiamDocID);
            db.AddInParameter(cmd, "@KeToanTruongID", SqlDbType.Int, this._KeToanTruongID);
            db.AddInParameter(cmd, "@NguoiNopTien", SqlDbType.NVarChar, this._NguoiNopTien);
            db.AddInParameter(cmd, "@NguoiLapPhieuID", SqlDbType.Int, this._NguoiLapPhieuID);
            db.AddInParameter(cmd, "@ThuQuyID", SqlDbType.Int, this._ThuQuyID);
            db.AddInParameter(cmd, "@DaNhanDuSoTien", SqlDbType.NVarChar, this._DaNhanDuSoTien);
            db.AddInParameter(cmd, "@TyGiaNgoaiTe", SqlDbType.Float, this._TyGiaNgoaiTe);
            db.AddInParameter(cmd, "@SoTienQuyDoi", SqlDbType.Float, this._SoTienQuyDoi);
            db.AddInParameter(cmd, "@DaKhoa", SqlDbType.Bit, this._DaKhoa);
            db.AddInParameter(cmd, "@USRC", SqlDbType.Int, this._USRC);
            db.AddOutParameter(cmd, "@NewThuChiID", SqlDbType.Int, 10);
            db.AddOutParameter(cmd, "@NewDATC", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);
            this._ThuChiID = (int)cmd.Parameters["@NewThuChiID"].Value;
            this._DATC = (DateTime)cmd.Parameters["@NewDATC"].Value;

            if (this._NgayLapPhieu != null)
            {
                this._Thang = this._NgayLapPhieu.Value.Month;
                this._Nam = this._NgayLapPhieu.Value.Year;
            }
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
            this._USRM = Session.LoginID;
            this._DATM = DateTime.Now;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("ThuChiBO_UpdateBO");
            db.AddInParameter(cmd, "@ThuChiID", SqlDbType.Int, this._ThuChiID);
            db.AddInParameter(cmd, "@LoaiPhieu", SqlDbType.Int, this._LoaiPhieu);
            db.AddInParameter(cmd, "@BoPhan", SqlDbType.NVarChar, this._BoPhan);
            db.AddInParameter(cmd, "@NgayLapPhieu", SqlDbType.DateTime, this._NgayLapPhieu);
            db.AddInParameter(cmd, "@NgayKyNhan", SqlDbType.DateTime, this._NgayKyNhan);
            db.AddInParameter(cmd, "@QuyenSo", SqlDbType.NVarChar, this._QuyenSo);
            db.AddInParameter(cmd, "@HoaDonBanID", SqlDbType.Int, this._HoaDonBanID);
            db.AddInParameter(cmd, "@So", SqlDbType.NVarChar, this._So);
            db.AddInParameter(cmd, "@No", SqlDbType.NVarChar, this._No);
            db.AddInParameter(cmd, "@Co", SqlDbType.NVarChar, this._Co);
            db.AddInParameter(cmd, "@KhachHangID", SqlDbType.Int, this._KhachHangID);
            db.AddInParameter(cmd, "@LaKhachHang", SqlDbType.Bit, this._LaKhachHang);
            db.AddInParameter(cmd, "@HoTen", SqlDbType.NVarChar, this._HoTen);
            db.AddInParameter(cmd, "@DiaChi", SqlDbType.NVarChar, this._DiaChi);
            db.AddInParameter(cmd, "@LyDo", SqlDbType.NVarChar, this._LyDo);
            db.AddInParameter(cmd, "@SoTien", SqlDbType.Float, this._SoTien);
            db.AddInParameter(cmd, "@ChungTuGocKemTheo", SqlDbType.NVarChar, this._ChungTuGocKemTheo);
            db.AddInParameter(cmd, "@GiamDocID", SqlDbType.Int, this._GiamDocID);
            db.AddInParameter(cmd, "@KeToanTruongID", SqlDbType.Int, this._KeToanTruongID);
            db.AddInParameter(cmd, "@NguoiNopTien", SqlDbType.NVarChar, this._NguoiNopTien);
            db.AddInParameter(cmd, "@NguoiLapPhieuID", SqlDbType.Int, this._NguoiLapPhieuID);
            db.AddInParameter(cmd, "@ThuQuyID", SqlDbType.Int, this._ThuQuyID);
            db.AddInParameter(cmd, "@DaNhanDuSoTien", SqlDbType.NVarChar, this._DaNhanDuSoTien);
            db.AddInParameter(cmd, "@TyGiaNgoaiTe", SqlDbType.Float, this._TyGiaNgoaiTe);
            db.AddInParameter(cmd, "@SoTienQuyDoi", SqlDbType.Float, this._SoTienQuyDoi);
            db.AddInParameter(cmd, "@DaKhoa", SqlDbType.Bit, this._DaKhoa);
            db.AddInParameter(cmd, "@USRM", SqlDbType.Int, this._USRM);
            db.AddInParameter(cmd, "@DELE", SqlDbType.Bit, this._DELE);

            db.AddOutParameter(cmd, "@NewDATM", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);

            this._DATM = (DateTime)cmd.Parameters["@NewDATM"].Value;

            if (this._NgayLapPhieu != null)
            {
                this._Thang = this._NgayLapPhieu.Value.Month;
                this._Nam = this._NgayLapPhieu.Value.Year;
            }
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_ThuChiID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("ThuChiBO_DeleteBO");
            db.AddInParameter(cmd, "@DirectDelete", SqlDbType.Bit, criteria.directDelete);
            db.AddInParameter(cmd, "@ThuChiID", SqlDbType.Int, criteria.ThuChiID);
            db.ExecuteNonQuery(cmd);
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        private void GetOldData()
        {
            Type t = this.GetType();
            MemberInfo[] mi = t.GetMembers();
            int length = mi.Length;
            string name = "";
            object objValue = null;
            string strValue = null;

            for (int i = 0; i < length; i++)
                if (mi[i].MemberType == MemberTypes.Property)
                {
                    name = mi[i].Name;
                    if (StaticUtil.Log_ValidColumn(name))
                    {
                        objValue = t.GetProperty(name).GetValue(this, null);
                        if (objValue != null)
                            strValue = objValue.ToString();
                        else
                            strValue = "";

                        hstOldValue.Add(name, strValue);
                    }
                }
        }

        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_ThuChiID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
